//
//  LLOAuthController.m
//  Weibo
//
//  Created by 李灵斌 on 15-2-1.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "LLOAuthController.h"
#import "LLAccountTool.h"
#import "LLTabBarController.h"
#import "LLWelComeViewController.h"
#import "LLNewFeatureController.h"
#import "LLControllerTool.h"
#import "LLHttpTool.h"
#define LLOAuthUrl @"https://api.weibo.com/oauth2/authorize"
#define LLRedirect_uri @"http://www.baidu.com"
#define LLOAuthAccess_tokenURL  @"https://api.weibo.com/oauth2/access_token"
#define LLGrant_type @"authorization_code"

@interface LLOAuthController ()<UIWebViewDelegate>

@end

@implementation LLOAuthController

- (void)loadView{
    
    self.view = [[UIWebView alloc] init];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    UIScrollView
    NSString *stringUrl = [NSString stringWithFormat:@"%@?client_id=%@&redirect_uri=%@", LLOAuthUrl, app_key,LLRedirect_uri];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringUrl]];
    UIWebView *webView = (UIWebView *)self.view;
    [webView loadRequest:request];
    webView.delegate = self;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    
}

- (void)dismiss{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UIWebViewDelegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    [SVProgressHUD dismiss];

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    // http://www.baidu.com/?code=efb9e1fdda4514231285ba8aa3c690a2
    NSString *absoluteString = request.URL.absoluteString;
//    
    NSRange range = [absoluteString rangeOfString:@"code="];
    if (range.location != NSNotFound ) {

        NSString *code = [absoluteString substringFromIndex:range.location + range.length];
        
        // 获得access_token
        [self getAccess_tokenCode:code];
        
        return NO;
    }
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD showWithStatus:@"正在加载"];
    LLLog(@"webViewDidStartLoad %@", webView.request.URL.absoluteString);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    LLLog(@"webViewDidFinishLoad %@", webView.request.URL.absoluteString);
    [SVProgressHUD dismiss];

}

// 获得access_token
- (void)getAccess_tokenCode:(NSString *)code{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"client_id"] = app_key;
    parameters[@"client_secret"] = app_secret;
    parameters[@"grant_type"] = LLGrant_type;
    parameters[@"code"] = code;
    parameters[@"redirect_uri"] = LLRedirect_uri;
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    NSSet *set = [NSSet setWithObject:@"text/plain"];
    mgr.responseSerializer.acceptableContentTypes = set;
    [mgr POST:LLOAuthAccess_tokenURL parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        // 通过框架字典转模型
        LLAccount *account = [LLAccount objectWithKeyValues:responseObject];
        [LLAccountTool save:account];
        [LLControllerTool chooseRootViewController];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD dismiss];
    }];
//    [LLHttpTool POST:LLOAuthAccess_tokenURL parameters:parameters success:^(id response) {
//        // 通过框架字典转模型
//        NSLog(@"%@", response);
//        LLAccount *account = [LLAccount objectWithKeyValues:response];
//        [LLAccountTool save:account];
//        [LLControllerTool chooseRootViewController];
//    } failure:^(NSError *error) {
//        [SVProgressHUD dismiss];
//    }];
}


@end
